﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using Microsoft.Xna.Framework;

namespace AwesomeGame.Networking
{
    public class CommonReadsAndWrites
    {
        public static void WriteVector(Vector2 vector, ref NetOutgoingMessage outgoingMessage)
        {
            outgoingMessage.Write(vector.X);
            outgoingMessage.Write(vector.Y);
        }

        public static Vector2 ReadVector(NetIncomingMessage incomingMessage)
        {
            Vector2 vector = new Vector2();
            vector.X = incomingMessage.ReadSingle();
            vector.Y = incomingMessage.ReadSingle();
            return vector;
        }

        public static void WriteGuid(Guid guid, ref NetOutgoingMessage outgoingMessage)
        {
            outgoingMessage.Write(guid.ToString());
        }

        public static Guid ReadGuid(NetIncomingMessage incomingMessage)
        {
            string guid = incomingMessage.ReadString();
            return new Guid(guid);
        }

        public static void WriteMessage(string message, ref NetOutgoingMessage outgoingMessage)
        {
            outgoingMessage.Write((int)DataMessageTypes.SAY);
            outgoingMessage.Write(message);
        }

        public static string ReadMessage(NetIncomingMessage incomingMessage)
        {
            return incomingMessage.ReadString();
        }

        public static void WriteListOfPlayers(List<Player> players, ref NetOutgoingMessage outgoingMessage)
        {
            outgoingMessage.Write((int)DataMessageTypes.FULL_PLAYER_LIST);
            outgoingMessage.Write(players.Count);
            foreach (Player player in players)
            {
                outgoingMessage.Write(player.Id);
                outgoingMessage.Write(player.Name);
                outgoingMessage.Write(player.Score);
            }
        }

        public static List<Player> ReadListOfPlayers(NetIncomingMessage incomingMessage)
        {
            int numberOfPlayers = incomingMessage.ReadInt32();
            List<Player> players = new List<Player>();
            for (int i = 0; i < numberOfPlayers; i++)
            {
                long id = incomingMessage.ReadInt64();
                string name = incomingMessage.ReadString();
                int score = incomingMessage.ReadInt32();
                players.Add(new Player(id, name, score));
            }

            return players;
        }

        public static void WritePlayer(Player player, ref NetOutgoingMessage outgoingMessage)
        {
            outgoingMessage.Write(player.Id);
            outgoingMessage.Write(player.Name);
            outgoingMessage.Write(player.Score);
        }

        public static Player ReadPlayer(NetIncomingMessage incomingMessage)
        {
            long id = incomingMessage.ReadInt64();
            string name = incomingMessage.ReadString();
            int score = incomingMessage.ReadInt32();

            Player player = new Player(id, name, score);

            return player;
        }
    }
}
